package com.zhuss.idea.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.idea.idea.DeleteIdeaAppendBo;
import com.zhuss.bean.bo.idea.idea.wechat.ForwardIdeaBo;
import com.zhuss.bean.vo.idea.idea.DetailIdeaAppendVo;
import com.zhuss.bean.vo.idea.idea.DetailIdeaVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消转发奇思妙想 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("ideaDelForward")
public class IdeaDelForward extends AbstractProcessor<Object, ForwardIdeaBo> implements Processor<Object, ForwardIdeaBo> {

    @Autowired
	@Qualifier("ideaRead")
	private IdeaRead ideaRead;
    
    @Autowired
	@Qualifier("ideaAppendRead")
	private IdeaAppendRead ideaAppendRead;
    
    @Autowired
    @Qualifier("ideaAppendDelete")
    private Processor<Object, DeleteIdeaAppendBo> ideaAppendDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(ForwardIdeaBo bo) throws WpsException {
    	Long ideaId = bo.getIdeaId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailIdeaAppendVo result = ideaAppendRead.getByType(appendType, ideaId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.奇思妙想未转发);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(ForwardIdeaBo param) throws WpsException {
    	Long ideaId = param.getIdeaId();
    	Integer appendType = param.getAppendType();
    	
    	DetailIdeaAppendVo result = ideaAppendRead.getByType(appendType, ideaId, param.getConcurrentUserId());
    	
    	DetailIdeaVo idea = ((DetailIdeaVo) ideaRead.detail(ideaId));
    	String name = idea.getTitle();
    	
    	DeleteIdeaAppendBo bo = new DeleteIdeaAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setIdeaAppendId(result.getIdeaAppendId());
    	ideaAppendDelete.process(bo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(ForwardIdeaBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.奇思妙想.key());
    	record.setAction(LogActionEnum.奇思妙想取消转发.key());
    	record.setTargetId(param.getIdeaId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
